﻿Imports System.Data.OleDb

Public Class SandCastle
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Assign text to the heading
        myHeading.InnerHtml = "SandCastle Contest"
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        'Check if user has added a sand castle. If yes the add button is disabled (members can add 1 sand castle)
        Dim SandCastleCountSql As String = "SELECT * FROM TotalSandCastlesPerTourist WHERE Tourist = @f1"
        Dim cmdCastleCount As OleDbCommand = New OleDbCommand(SandCastleCountSql, oleDbConn)
        cmdCastleCount.CommandType = CommandType.Text
        cmdCastleCount.Parameters.AddWithValue("@f1", User.Identity.Name)

        Dim drCastleCount As OleDbDataReader = cmdCastleCount.ExecuteReader()
        If drCastleCount.HasRows = True Then
            drCastleCount.Read()
            If (drCastleCount("TotalCastles") = 1) Then
                btn_SandCastle.Enabled = False
                btn_SandCastle.Text = "Joined"
            End If
        End If

        If IsPostBack = False Then
            'Count Activity for page
            Dim SqlStringActivity As String = "INSERT INTO SiteActivity(Activity) VALUES (@f1)"
            Dim cmdActivity As OleDbCommand = New OleDbCommand(SqlStringActivity, oleDbConn)
            cmdActivity.CommandType = CommandType.Text
            cmdActivity.Parameters.AddWithValue("@f1", "SandCastles")
            cmdActivity.ExecuteNonQuery()
            oleDbConn.Close()
        End If
    End Sub

    Protected Sub btn_findByName_Click(sender As Object, e As EventArgs) Handles btn_findByName.Click
        searchDatabase("CastleName", tb_search.Text)
    End Sub

    Private Function searchDatabase(ByVal buttonStr As String, ByVal searchStr As String)
        SqlDataSource2.SelectCommand = "SELECT * FROM TotalVotesPerCastle WHERE " & buttonStr & " LIKE '%" & searchStr & "%'"
        SqlDataSource2.DataBind()
        Return True
    End Function

    Protected Sub SqlDataSource2_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        'If data source is empty
        If (e.AffectedRows < 1) Then
            myHeading.InnerHtml = "No SandCastle found"
        End If
        If (e.AffectedRows < 13) Then
            DataPagerBeaches.Visible = False
        Else
            DataPagerBeaches.Visible = True
        End If
    End Sub

    Protected Sub ListView1_DataBound(sender As Object, e As EventArgs)
        'Display datapager only if there is more than one page to display
        DataPagerBeaches.Visible = (DataPagerBeaches.PageSize < DataPagerBeaches.TotalRowCount)
    End Sub
End Class